import os
import hashlib
from PIL import Image
import imagehash
import shutil


#去除重复图片
# 设置文件夹路径
source_folder = r"D:\output\vcg_photos"
result_folder = r"D:\output\vcg_photos_result"

# 确保目标文件夹存在
if not os.path.exists(result_folder):
    os.makedirs(result_folder)

# 用于存储图片的哈希值，判断是否重复
hashes = {}

# 遍历文件夹内的所有图片文件
for filename in os.listdir(source_folder):
    file_path = os.path.join(source_folder, filename)

    if os.path.isfile(file_path):
        try:
            # 读取图片并计算哈希值
            img = Image.open(file_path)
            hash_value = imagehash.average_hash(img)

            # 如果哈希值已存在，表示图片重复，跳过
            if hash_value in hashes:
                print(f"跳过重复图片: {filename}")
                continue

            # 如果哈希值不重复，保存图片到目标文件夹
            hashes[hash_value] = filename
            shutil.copy(file_path, os.path.join(result_folder, filename))
            print(f"保存图片: {filename}")

        except Exception as e:
            print(f"无法处理图片 {filename}: {e}")

print("图片去重完成！")